package com.speechb.controller.web;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.speechb.common.text.PageUtil;
import com.speechb.controller.base.BaseController;
import com.speechb.entity.Blog;
import com.speechb.service.IBlogService;
import com.speechb.service.IMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author : llj
 * @version :2.1.0
 * @date : 2020/6/14 9:33
 */
@Controller
public class BlogWebController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(BlogWebController.class);

    @Autowired
    IBlogService blogService;

    @Autowired
    IMenuService menuService;

    private static final int PAGE_SIZE = 12;

    @GetMapping(value = {"/{id}"})
    public ModelAndView index(@PathVariable Integer id) {

        Page pageHelper = PageHelper.startPage(1, PAGE_SIZE, true);
        List<Blog> blogList = blogService.findAll(id);
        blogService.convertConverUrl(blogList);

        ModelAndView mav = new ModelAndView("web/app/index");
        mav.addObject("index", PageUtil.getPageInd(1, id, pageHelper.getPages(), "/"));
        mav.addObject("list", blogList);
        mav.addObject("menus", menuService.menulist());
        return mav;
    }


    @GetMapping(value = {"/{id}/{pageNum}"})
    public ModelAndView index(@PathVariable Integer id, @PathVariable Integer pageNum) {

        Page pageHelper = PageHelper.startPage(pageNum, PAGE_SIZE, true);
        List<Blog> blogList = blogService.findAll(id);
        blogService.convertConverUrl(blogList);

        ModelAndView mav = new ModelAndView("web/app/index");
        mav.addObject("index", PageUtil.getPageInd(pageNum, id, pageHelper.getPages(), "/"));
        mav.addObject("list", blogList);
        mav.addObject("menus", menuService.menulist());
        return mav;
    }
}
